package com.example.demo.Service.lmp;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.Dto.*;
import com.example.demo.Mapper.UserMapper;
import com.example.demo.Service.UserService;
import com.example.demo.Util.JWTUtil;
import com.example.demo.Util.ThreadLocallUtil;
import com.example.demo.VO.LoginVo;
import com.example.demo.VO.RegisterVo;

import com.example.demo.VO.UserInfoVo;
import com.example.demo.Enum.UserStatusEnum;
import com.example.demo.po.User;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class UserServicelmp  extends ServiceImpl<UserMapper, User> implements UserService{


    @Autowired
private UserMapper userMapper;
    @Autowired
    private RegisterVo registerVo;
    @Autowired
    private LoginVo loginVo;
    @Autowired
    private UserInfoVo userInfoVo;


    public RegisterVo register(RegisterDto registerDto) {
        User user = new User();
        user.setEmail(registerDto.getEmail());
        user.setPassword(registerDto.getPassword());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setStatus(UserStatusEnum.NORMAL);
        if (!save( user)){
            throw new RuntimeException("注册失败");
        }
        registerVo.setUserId(user.getUserId());
        return registerVo;
    }

    public LoginVo login(LoginDto loginDto) {
        User one = getOne(new QueryWrapper<User>().eq("email", loginDto.getEmail()).eq("password", loginDto.getPassword()));
        Long userId = one.getUserId();
        if (userId==null||userId==0){
          return null;
      }
        String jwt = JWTUtil.createJWT(userId);
        loginVo.setUserId(userId);
       loginVo.setAccessToken(jwt);
       loginVo.setRefreshToken(jwt);
        return loginVo;
    }


    public LoginVo refresh(String refreshToken) {
        Claims claims = JWTUtil.parseJWT(refreshToken);
        Object userId = claims.get("userId");
        Long refresh = userMapper.refresh(userId);
        if (refresh==null){
            return null;
        }
        String jwt = JWTUtil.createJWT(userId);
        loginVo.setUserId(Long.parseLong(userId.toString()));
        loginVo.setAccessToken(refreshToken);
        loginVo.setRefreshToken(jwt);
    return loginVo;

    }


    public UserInfoVo getuser(User user) {
        User user1 = userMapper.getUser(user);
        BeanUtil.copyProperties(user1,userInfoVo);
        return userInfoVo;
    }


    public void delect(LogoffDto logoffDto) {
        User user = ThreadLocallUtil.getUser();
        Long userId = user.getUserId();
        int updatestatus = userMapper.updatestatus(logoffDto, userId);
        if (updatestatus==0){
           throw new RuntimeException("注销失败");
        }

    }



    public void updatestatus(UserUpdateDto userUpdateDto, Long userId) {
        int updatestatus = userMapper.updatestatus1(userUpdateDto, userId);
        if (updatestatus==0){
            throw new RuntimeException("修改失败");
        }
    }


    public void updatestatus(BannedDto bannedDto) {

    }
}